Skip to content

fix: report blocks() in 512-byte units per POSIX stat(2)#348

Draft
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-blocks-512-units
Draft

fix: report blocks() in 512-byte units per POSIX stat(2)#348
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-blocks-512-units

Conversation

@toddr-bot
Copy link
Copy Markdown
Collaborator

What

blocks() now returns st_blocks in 512-byte units, matching real POSIX stat(2) behavior.

Why

The previous implementation returned the count of filesystem blocks (ceil(size/blksize)) — but stat(2) always reports st_blocks in 512-byte units regardless of st_blksize. A 5-byte file with blksize=4096 should have stat[12] = 8 (one 4096-byte block = 8 × 512), not 1. Any code under test relying on $stat[12] for space calculations (e.g., du, quota checks, File::Find::Rule) would get wrong answers from mocked files.

How

Multiply the filesystem block count by blksize / 512. Updated t/blocks.t, t/Test-MockFile_file.t, and t/mock_stat.t to assert the correct values.

Testing

Full test suite passes (94 files, 1587 tests). Only pre-existing t/fh-ref-leak.t failure (GH #179).

Verified against real Perl: stat() on a 5-byte tempfile returns blocks=8 with blksize=4096.


🤖 Generated with Claude Code

blocks() was computing the number of filesystem blocks (ceil(size/blksize))
but stat(2) st_blocks is always reported in 512-byte units regardless of
st_blksize. A 5-byte file with blksize=4096 should report blocks=8, not 1.

Updated blocks() to multiply by (blksize/512) and fixed tests that
encoded the incorrect expectations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant